package com.yiibai.spring.controller;

import com.yiibai.dao.DAO;
import com.yiibai.spring.bean.Product;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
//@RequestMapping("/a")
public class MyController {

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "index");
        return "index";
    }

    @GetMapping("/3")
    public String index3(Model model) {
        model.addAttribute("message", "Hello Spring MVC 5!");
        return "index3";
    }

    @GetMapping("/4")
    public String index4(Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Product product = new Product("花生油", 129, sdf.parse("2018-02-18"));
        model.addAttribute("product", product);
        return "index4";
    }

    @GetMapping("/5")
    public String index5(Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Product product = new Product("花生油", 129, sdf.parse("2018-02-18"));
        model.addAttribute("product", product);
        return "index5";
    }

    @GetMapping("/6")
    public String index6(Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Product product = new Product("花生油", 129, sdf.parse("2018-02-18"));
        model.addAttribute("product", product);
        return "index6";
    }

    @GetMapping("/7")
    public String index7(Model model) throws ParseException {
        String html =  "Welcome to our <b>fantastic</b> grocery store!";
        model.addAttribute("html", html);
        return "index7";
    }

    @GetMapping("/8")
    public String index8(Model model) throws ParseException {
        List productList = DAO.loadAllProducts();
        model.addAttribute("productList", productList);
        return "index8";
    }

    @GetMapping("/9")
    public String index9(Model model) throws ParseException {
        List productList = DAO.loadAllProducts();
        model.addAttribute("productList", productList);
        return "index9";
    }

    @GetMapping("/10")
    public String index10(Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Product product = new Product("huashengyou", 10, sdf.parse("2021-03-19"));
        model.addAttribute("product", product);
        return "index10";
    }

    @RequestMapping(value = { "/saveProduct" }, method = RequestMethod.POST)
    public String saveprodcut(Model model, String name) throws ParseException {
        //List productList = DAO.loadAllProducts();
        //model.addAttribute("productList", productList);
        return "index10";
    }

}